﻿using UFSoft.UBF.Eventing;


namespace ufida.u9.cust.dz.beplugin.Receivement
{
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using ufida.u9.cust.dz.beplugin.JkySchema;
    using ufida.u9.cust.dz.beplugin.Utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.InvDoc.TransferIn;
    using UFIDA.U9.PM.Rcv;
    using UFIDA.U9.SCM.PM.PSS;
    using UFSoft.UBF.Util.Context;
    using ufida.u9.cust.dz.beplugin.loger;
   

    class AfterUpdated : IEventSubscriber
    {
        public void Notify(params object[] args)
        {


            if (PlatformContext.Current.EnterpriseID.Equals("10")) {
                Receivement entity = null;
                if (BaseCommon.TryGetEntity(args, ref entity))
                {
                    //判断状态
                    RcvStatusEnum status = entity.Status;
                    RcvStatusEnum Original = entity.OriginalData.Status;
                   

                        if (status == RcvStatusEnum.Closed && Original == RcvStatusEnum.Approving)
                        {
                        if (entity.RcvLines[0].Wh != null ) {
                            if (entity.RcvLines[0].Wh.DescFlexField.PrivateDescSeg1.Equals("True"))
                            {
                                LogUtil logUtil = new LogUtil();
                                OutReceivement outReceivement = GetJkyReceivementi(entity);
                                string jsonObj = JsonConvert.SerializeObject(outReceivement);
                                WebResult res = Task.Run(() => JkyUtils.Post("erp.stock.createandstockin", jsonObj)).GetAwaiter().GetResult();
                                if (res.Sucess)
                                {

                                    JkyResponse jkyResponse = JsonConvert.DeserializeObject<JkyResponse>(res.Data);

                                    if (!jkyResponse.code.Equals("200"))
                                    {
                                        logUtil.RecordLog(entity.DocNo, "入库单传送", outReceivement, 0, jkyResponse.msg);
                                        throw new Exception("采购入库推送吉克云失败:" + jkyResponse.msg);
                                    }
                                    else
                                    {
                                        logUtil.RecordLog(entity.DocNo, "入库单传送", outReceivement, 1, "推送成功");
                                    }

                                }
                                else
                                {
                                    logUtil.RecordLog(entity.DocNo, "入库单传送", outReceivement, 0, JsonUtils.Serialize(res));
                                    throw new Exception("采购入库推送失败");
                                }
                            }
                        }

             
                        }
                    

                }

            }

        }

        private OutReceivement GetJkyReceivementi(Receivement entity)
        {
            OutReceivement outReceivement = new OutReceivement();


            outReceivement.relDataId = entity.DocNo;
            outReceivement.applyCompanyCode = "00" + entity.RcvLines[0].OwnOrg.Code;
           // outReceivement.applyDepartCode = entity.RcvLines[0].RcvDept.Code+"-10";
            outReceivement.inWarehouseCode = entity.RcvLines[0].Wh.Code;
            outReceivement.applyDate = entity.RcvLines[0].ConfirmDate.ToString("yyyy-MM-dd HH:mm:ss");
            outReceivement.auditDate = entity.RcvLines[0].ConfirmDate.ToString("yyyy-MM-dd HH:mm:ss");
            outReceivement.planInDate = entity.RcvLines[0].ConfirmDate.ToString("yyyy-MM-dd HH:mm:ss");
            outReceivement.applyUserName = entity.RcvLines[0].WhMan == null ? "" : entity.RcvLines[0].WhMan.Name;
            outReceivement.applyCompanyCode = entity.RcvLines[0].RcvDept == null ? "" : entity.RcvLines[0].RcvDept.Code;
            outReceivement.@operator = Context.LoginUser;

            outReceivement.stockInDetailViews = new List<stockInDetailViews>();

            foreach (var line in entity.RcvLines)
            {
                stockInDetailViews jkyLine = new stockInDetailViews();

                jkyLine.outSkuCode = line.ItemInfo.ItemCode;
                jkyLine.skuCount = line.RcvQtyTU;
                jkyLine.relDetailId = line.ID.ToString();

                //批号
                if (line.InvLot != null && line.InvLot.LotCode != null)
                {
                    jkyLine.batchList = new List<batchList>() { new batchList() { batchNo = line.InvLot.LotCode, quantity = line.RcvQtyTU } };
                   
                }
                //序列号 一个料品对应一个序列号对象 
               /* jkyLine.serialList = new List<serialList>();

                for (int i = 1; i <= line.RcvQtyTU; i++)
                {
                    long milliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                    jkyLine.serialList.Add(new serialList() { serialNo = line.ItemInfo.ItemCode + "-"+ milliseconds.ToString() + "-" + i  });
                    }*/
                //序列号
                outReceivement.stockInDetailViews.Add(jkyLine);
            }
            return outReceivement;
        }
    }

}
